﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using HotelManagement.Class;
using System.Data.SqlClient;
using System.Configuration;
namespace HotelManagement
{
    public partial class UpdateStaff : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                displayStaffInformation();
            }
        }

        protected void btnGoBack_Click(object sender, EventArgs e)
        {
            Server.Transfer("ManageHousekeepingAndStaff.aspx");
        }

        protected void btnClearUpdate_Click(object sender, EventArgs e)
        {
            clearTextBox();
        }

        protected void btnUpdateStaff_Click(object sender, EventArgs e)
        {
            updateStaffInformation();
        }

        private void displayStaffInformation()
        {
            //Get query string 

            string qs = Request.QueryString["StaffID"];

            //Call getstaffbyid method
            Staff s = StaffDBManager.getStaffById(qs);
            BankAccountStaff ba = BankAccountStaffDBManager.getBankAccountbyStaffId(qs);

            //Display the General Information in the textbox
            lblStaffID.Text = s.StaffID;
            tbxFirstName.Text = s.FirstName;
            tbxLastName.Text = s.LastName;
            rblGender.SelectedValue = s.Gender;
            ddlDutyType.SelectedValue = s.DutyType;
            tbxTitle.Text = s.Title;

            displayReportToItems();

            ddlReportTo.Text = s.ReportTo;
            DateTime GetDay = s.DOB;
            string Day = Convert.ToString(GetDay.Day);
            tbxDay.Text = Day;

            DateTime GetYear = s.DOB;
            string Year = Convert.ToString(GetYear.Year);
            tbxYear.Text = Year;

            DateTime GetMonth = s.DOB;
            string Month = Convert.ToString(GetMonth.Month);
            ddlMonth.SelectedValue = Month;


            //Display Conntact Information in the textbox
            tbxMobileNumber.Text = s.MobileNum.ToString();
            tbxHomeNumber.Text = s.HomeNum.ToString();
            tbxEmail.Text = s.Email;
            tbxBlockNum.Text = s.BlockNum;
            tbxStreetName.Text = s.StreetName;
            tbxUnitNum.Text = s.UnitNum;
            tbxZIPCode.Text = s.PostalCode;
            tbxCountry.Text = s.Country;

            //Display Bank Account Information
            tbxAccountName.Text = ba.AccountName;
            ddlAccountType.SelectedValue = ba.AccountType;
            tbxAccountNumber.Text = ba.AccountNumber;
            tbxTransitCode.Text = ba.TransitCode;
            tbxBankName.Text = ba.BankName;
            tbxBankBranch.Text = ba.BankBranch;

            //Display Additional Information into the textbox
            tbxNotes.Text = s.Notes;
        }

        private void updateStaffInformation()
        {
            try
            {

                DateTime today = DateTime.Today;
                int dOBYear = (int)Convert.ToInt32(tbxYear.Text);
                int age = today.Year - dOBYear;

                //Call Update method
                Staff updateStaff = new Staff();
                BankAccountStaff updateBankAccountStaff = new BankAccountStaff();

                //Update General information
                updateStaff.StaffID = lblStaffID.Text;
                updateStaff.DutyType = ddlDutyType.SelectedValue;
                updateStaff.Title = tbxTitle.Text;
                updateStaff.ReportTo = ddlReportTo.Text;
                updateStaff.FirstName = tbxFirstName.Text;
                updateStaff.LastName = tbxLastName.Text;
                updateStaff.Age = age;
                updateStaff.DOB = Convert.ToDateTime(tbxYear.Text + "/" + ddlMonth.SelectedValue + "/" + tbxDay.Text);
                updateStaff.Gender = rblGender.SelectedValue;

                //Update Contact Information
                updateStaff.MobileNum = Convert.ToInt32(tbxMobileNumber.Text);
                updateStaff.HomeNum = Convert.ToInt32(tbxHomeNumber.Text);
                updateStaff.Email = tbxEmail.Text;

                updateStaff.BlockNum = tbxBlockNum.Text;
                updateStaff.StreetName = tbxStreetName.Text;
                updateStaff.UnitNum = tbxUnitNum.Text;
                updateStaff.PostalCode = tbxZIPCode.Text;
                updateStaff.Country = tbxCountry.Text;

                //Update Bank Account Information
                updateBankAccountStaff.AccountName = tbxAccountName.Text;
                updateBankAccountStaff.AccountType = ddlAccountType.SelectedValue;
                updateBankAccountStaff.AccountNumber = tbxAccountNumber.Text;
                updateBankAccountStaff.TransitCode = tbxTransitCode.Text;
                updateBankAccountStaff.BankName = tbxBankName.Text;
                updateBankAccountStaff.BankBranch = tbxBankBranch.Text;
                updateBankAccountStaff.StaffID = lblStaffID.Text;

                //Update Additional Information
                updateStaff.Notes = tbxNotes.Text;

                //Call the Update Staff method and pass the object  to the method
                //Display message of success or failure to user
                if (StaffDBManager.updateStaff(updateStaff) == 1 && BankAccountStaffDBManager.updateBankAccountStaff(updateBankAccountStaff) == 1)
                {
                    lblStatus.Text = "Details update successfully";
                }
                else
                {
                    lblStatus.Text = "Update failed. Please try again";
                }
            }
            catch (Exception ex)
            {
                lblStatus.Text = "Error: " + ex.Message;

            }
        }

        private void displayReportToItems()
        {
            //Display Report to items into the Dropdownlist
            ddlReportTo.Items.Clear();
            string selectSQL = "SELECT FirstName,LastName FROM Staff";
            SqlConnection con = null;
            SqlDataReader reader;

            try
            {
                con = new SqlConnection();
                con.ConnectionString = ConfigurationManager.ConnectionStrings["DelonixRegiaDBConnectionString"].ConnectionString;
                SqlCommand cmd = new SqlCommand(selectSQL, con);
                ListItem newItem = new ListItem();


                con.Open();
                reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    newItem = new ListItem();
                    newItem.Text = reader["FirstName"].ToString() + " " + reader["LastName"].ToString();
                    newItem.Value = reader["FirstName"].ToString() + " " + reader["LastName"].ToString();
                    ddlReportTo.Items.Add(newItem);
                }
                reader.Close();

            }
            catch (SqlException ex1)
            {
                lblStatus.Text = "Error: " + ex1 + "Please try again";
            }
        }

        private void clearTextBox()
        {
            // Clear General Information
            tbxTitle.Text = String.Empty;
            tbxFirstName.Text = String.Empty;
            tbxLastName.Text = String.Empty;
            tbxDay.Text = String.Empty;
            tbxYear.Text = String.Empty;

            //Clear Contact Information
            tbxMobileNumber.Text = String.Empty;
            tbxHomeNumber.Text = String.Empty;
            tbxEmail.Text = String.Empty;
            tbxBlockNum.Text = String.Empty;
            tbxStreetName.Text = String.Empty;
            tbxUnitNum.Text = String.Empty;
            tbxCountry.Text = String.Empty;
            tbxZIPCode.Text = String.Empty;

            //Clear Bank Account Information
            tbxAccountName.Text = String.Empty;
            tbxAccountNumber.Text = String.Empty;
            tbxTransitCode.Text = String.Empty;
            tbxBankName.Text = String.Empty;
            tbxBankBranch.Text = String.Empty;

            //Clear Additional Information
            tbxNotes.Text = String.Empty;

            //Clear label
            lblStatus.Text = String.Empty;
        }














       

       

   

       
      
        }
    }
